package labb3;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.URL;
import javax.swing.*;
class gameBoard extends JPanel {

    URL loadUrl1 = gameBoard.class.getResource("rock.gif");
    private ImageIcon imageicon1 = new ImageIcon(loadUrl1);
    URL loadUrl2 = gameBoard.class.getResource("paper.gif");
    private ImageIcon imageicon2 = new ImageIcon(loadUrl2);
    URL loadUrl3 = gameBoard.class.getResource("scissors.gif");
    private ImageIcon imageicon3 = new ImageIcon(loadUrl3);
    
    private ImageIcon[] icons = {imageicon1, 
		    imageicon2,imageicon3};
    

    private JButton[] buttons = new JButton[3];
    private JButton lastPlayed; // remembers last chosen button/gesture
    private String[] texts = {"STEN", "PASE", "SAX"};
    private JLabel upperMessage, lowerMessage, scorelabel;
    private int score;
    private Color bgcolor;
    HashMap<String,JButton> map = new HashMap<String,JButton>();

    // Constructor that builds the board, used for computers board
    gameBoard(String name) {
	setLayout(new GridLayout(5,1));

	// Upper JPanel holds players name and last gesture played
	JPanel upper = new JPanel();
	upper.setLayout(new GridLayout(2,1));
	upperMessage = new JLabel("RPS", JLabel.CENTER);
	upper.add(new JLabel(name, JLabel.CENTER)); 
	upper.add(upperMessage);
	add(upper);

	// Lower JPanel has messages about the game and score
	JPanel lower = new JPanel();
	lower.setLayout(new GridLayout(2,1));
	lowerMessage = new JLabel("win/lose/draw", JLabel.CENTER);
	scorelabel = new JLabel("Score: 0", JLabel.CENTER);
	lower.add(lowerMessage); lower.add(scorelabel);

	for (int i = 0; i<3; i++){
	    buttons[i] = new JButton(icons[i]);
            buttons[i].setPreferredSize(new Dimension(100,110));
	    buttons[i].setActionCommand(texts[i]);
	    add(buttons[i]);
	    map.put(texts[i],buttons[i]);
	}
	add(lower); // added after buttons
	bgcolor = buttons[0].getBackground();
	lastPlayed = buttons[0]; // arbitrary value at start
    }


    // Contructor for players board, puts listener on buttons
    gameBoard(String name, ActionListener listener) {
	this(name); // call other constructor to build the board
	for (int i = 0; i<3; i++)
	    buttons[i].addActionListener(listener);
    }

    // reset yellow color
    void resetColor() {
	lastPlayed.setBackground(bgcolor);
    }

    void setUpper(String r) {
	
    }

    void setLower(String r) {
	lowerMessage.setText(r);
    }

   // remember last chosen JButton and mark it yellow
    void markPlayed(String r) {
	lastPlayed = map.get(r); 
	lastPlayed.setBackground(Color.yellow);
        upperMessage.setText(r);
    }

    // add one point and display new score
    void wins() {
	score++;
	scorelabel.setText("Score: " + score);
    }
}
